﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace QuanLyKhachSan_DAL_WS
{
    public class XuLyBaoCao
    {
        // lay toan bo bao cao
        public static DataTable TimToanBoBaoCao()
        {
            DataTable dt = new DataTable();

            CDataProvider reader = new CDataProvider();

            String sql = "SELECT * FROM BAOCAO ";
            try
            {
                dt = reader.DocDuLieu(sql);
            }
            catch (System.Exception )
            {
            	
            }

            return dt;
        }
        // lay bao cao theo nam
        public static DataTable TimBaoCaoNam(int Nam)
        {
            DataTable dt = new DataTable();

            CDataProvider reader = new CDataProvider();

            String sql = "SELECT * FROM BAOCAO WHERE YEAR(NgayLapBaoCao) = " + Nam.ToString() + " ";
            try
            {
                dt = reader.DocDuLieu(sql);
            }
            catch (System.Exception)
            {

            }

            return dt;
        }
        // lay bao cao theo thang , nam
        public static DataTable TimBaoCaoThang(int Thang,int Nam)
        {
            DataTable dt = new DataTable();

            CDataProvider reader = new CDataProvider();

            String sql = "SELECT * FROM BAOCAO WHERE MONTH(NgayLapBaoCao) = " + Thang.ToString() + " AND YEAR(NgayLapBaoCao) = " + Nam.ToString() + " ";
            try
            {
                dt = reader.DocDuLieu(sql);
            }
            catch (System.Exception)
            {

            }

            return dt;
        }
        // tim 1 bao cao theo ngay hien tai
        public static CBAOCAO TimBaoCao(DateTime HienTai)
        {
            CBAOCAO bc = new CBAOCAO();

            CDataProvider reader = new CDataProvider();

            String sql = "SELECT * FROM BAOCAO WHERE NgayLapBaoCao = '"+ HienTai.ToShortDateString() +"'";

            try
            {
                DataTable dt = reader.DocDuLieu(sql);

                if (dt.Rows.Count == 1)
                {
                    DataRow dr = dt.Rows[0];

                    bc.MaBaoCao = int.Parse(dr["BaoCaoID"].ToString());
                    bc.NgayLapBaoCao = DateTime.Parse(dr["NgayLapBaoCao"].ToString());
                    bc.SoLuongKhach = int.Parse(dr["TongSoLuongKhach"].ToString());
                    bc.SoLuongPhong = int.Parse(dr["TongSoPhong"].ToString());
                    bc.DoanhThuNgay = long.Parse(dr["DoanhThuNgay"].ToString());
                }
                              
            }
            catch (System.Exception )
            {
            	
            }

            return bc;
        }
        // them 1 bao cao
        public static Boolean ThemBaoCao(CBAOCAO bc)
        {
            CDataProvider writer = new CDataProvider();

            String insertsql = "INSERT INTO BAOCAO(NgayLapBaoCao,TongSoLuongKhach,TongSoPhong,DoanhThuNgay) "
                + "VALUES('" + bc.NgayLapBaoCao.ToShortDateString() + "'," + bc.SoLuongKhach.ToString() + ","
                + " "+ bc.SoLuongPhong.ToString() +","+ bc.DoanhThuNgay.ToString() +") ";

            if (writer.ThucThiDuLieu(insertsql))
            {
                return true;
            }
            else
                return false;
        }
        // sua 1 bao cao
        public static Boolean SuaBaoCao(CBAOCAO bc,int MaBC)
        {
            CDataProvider writer = new CDataProvider();

            String updatesql = "UPDATE BAOCAO SET "
                + "NgayLapBaoCao='" + bc.NgayLapBaoCao.ToShortDateString() + "' TongSoLuongKhach=" + bc.SoLuongKhach.ToString() + " "
                + "TongSoPhong=" + bc.SoLuongPhong.ToString() + " DoanhThuNgay=" + bc.DoanhThuNgay.ToString() + " "
                + "WHERE BaoCaoID="+ MaBC.ToString() +" ";

            if (writer.ThucThiDuLieu(updatesql))
            {
                return true;
            }
            else
                return false;
        }
        // xoa 1 bao cao
        public static Boolean XoaBaoCao(int MaBC)
        {
            CDataProvider writer = new CDataProvider();

            String deletesql = "DELETE FROM BAOCAO "
                + "WHERE BaoCaoID=" + MaBC.ToString() + " ";

            if (writer.ThucThiDuLieu(deletesql))
            {
                return true;
            }
            else
                return false;
        }
    }
}
